package org.shan.hangtian.properties;

import com.mlnx.common.utils.MyLog;

import org.shan.hangtian.utils.PathUtils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Created by amanda.shan on 2020/10/2.
 */
public class HangtianProperties {

    MyLog log = MyLog.getLog(getClass());

    private static HangtianProperties instance = new HangtianProperties();

    public static HangtianProperties getInstance() {
        return instance;
    }

    private Properties properties = new Properties();

//    private Integer port;
//
//    private String ip;
//
//    private int timeOut;
//
//    private String path = "D://data/";

    public HangtianProperties() {

        try {
            File file = new File(getConfigPath());
            if (file.exists()) {
                properties.load(new FileReader(file));
            }
        } catch (IOException e) {
            log.error(e, "打开配置文件异常");
        }
    }

    public String getConfigPath() {
        return "D:\\医学实验项目软件\\脑疲劳恢复仪\\脑疲劳恢复仪\\config.properties";
//        return PathUtils.getPath("config.properties");
    }

    public Integer getPort() {
        return Integer.valueOf(properties.getProperty("port", "18080"));
    }

    public void setPort(Integer port) throws IOException {
        properties.setProperty("port", port.toString());

        store();
    }

    public int getTimeOut() {
        return Integer.valueOf(properties.getProperty("timeOut", "2"));
    }

    public void setTimeOut(int timeOut) throws IOException {
        properties.setProperty("timeOut", timeOut + "");
        store();
    }

    public void setPath(String path) throws IOException {
        properties.setProperty("path", path.toString());

        store();
    }

    public String getPath() {
        return properties.getProperty("path", "D:\\data");
    }

    public String getIp() {
        return properties.getProperty("ip", "192.168.1.100");
    }

    public void setIp(String ip) throws IOException {
        properties.setProperty("ip", ip.toString());
        store();
    }

    private void store() throws IOException {
        File file = new File(getConfigPath());
        System.out.println(file.getPath());
        properties.store(new FileWriter(file), "修改");
    }
}
